<?php

/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/17
 * Time: 下午10:38
 */
namespace App\Busi\Modules\Captcha;
use App\Busi\Modules\BaseBusi;
use App\Exceptions\CaptchaException;
use App\Exceptions\ErrorCode\CaptchaError;
use App\Http\Requests\CaptchaConsultRo;
use App\Util\StringUtil;
use App\Busi\Traits\SmsTrait;
use App\Busi\Traits\CaptchaTrait;
class ConsultCaptchaBusi extends BaseBusi
{
    private $captcha_key = "captcha:consult:";
    use SmsTrait,CaptchaTrait;
    public function handle(CaptchaConsultRo $captchaRo)
    {

        if(StringUtil::isStrValid($captchaRo->phone, StringUtil::MobileString) == false) {
            throw new CaptchaException(CaptchaError::CAPTCHA_PHONE_ERROR);
        }

        $captcha = StringUtil::getIntCaptcha(6);

        $templates = ['Aliyun'=>'SMS_132400102'];
        $tempData = [
            'code'=>$captcha,
        ];

        $result = $this->sendTemplateSms($captchaRo->phone,$templates,$tempData);

        $ret =$this->setCaptcha($this->captcha_key.$captchaRo->phone, $captcha);

        if($result['success'] == 'true'){
            return ['msg'=>'发送成功'];
        }else{
            throw new CaptchaException(CaptchaError::REPEAT_REQUEST_CAPTCHA);
        }

    }
}